package com.bamanker.sms_service.service;

import com.bamanker.sms_service.pojo.toEmail;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author bamanker
 */
@Service
@Slf4j
public class MyMailService {

    @Autowired(required = false)
    private JavaMailSender javaMailSender;

    @Autowired
    private ToEmailService service;

    public static final String FROM = "27054792@qq.com";

    public void sendMail(String subject, String text) {
        SimpleMailMessage smm = new SimpleMailMessage();
        List<toEmail> emails = service.list();
        log.info("emails:{}",emails);
        String[] to = new String[emails.size()];
        for (int i = 0; i < emails.size(); i++) {
            to[i]=emails.get(i).getEmail();
        }
        smm.setFrom(FROM);//发送者
        smm.setTo(to);//收件人
        smm.setCc(to);//抄送人
        smm.setSubject(subject);//邮件主题
        smm.setText(text);//邮件内容
        javaMailSender.send(smm);//发送邮件
    }


}
